package com.itistimetothink.cjetso;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;

public class Splash extends Activity {
	
	protected int _splashTime = 3000;//3 seconds
    private Thread splashTread;
    private static boolean checked =false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    	Log.i("in the onCreate","Splash");

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if(!isNetworkConnected()){
        	checked =false;
        	Intent intent = new Intent(Intent.ACTION_MAIN);
        	intent.setClassName("com.android.phone", "com.android.phone.Settings");
        	startActivity(intent);
        }

        else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&&!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        	Toast.makeText(this, "Please enable location service", Toast.LENGTH_SHORT).show();
        	checked =false;
        	Intent callGPSSettingIntent = new Intent(
        			android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        	startActivity(callGPSSettingIntent);
        }
        else{        	
            splashTread = new Thread() {
                @SuppressWarnings("finally")
				@Override
                public void run() {
                    try {
                        synchronized(this){

                                wait(_splashTime);
                        }

                    } catch(InterruptedException e) {}
                    finally {
                        finish();
                    	Log.i("in the thread","finished");
                        //start a new activity
                        Intent i = new Intent(Splash.this, MainActivity.class);
                        startActivity(i);
                        
                        return;
                    }
                }
            };
            checked =true;
            splashTread.start();
        }
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_splash, menu);
        return true;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN&&checked) {
            synchronized(splashTread){
                    splashTread.notifyAll();
            }
        }
        return true;
    }
    
    private boolean isNetworkConnected() {
    	  ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    	  NetworkInfo ni = cm.getActiveNetworkInfo();
    	  if (ni == null) {
    	   // There are no active networks.
    	   return false;
    	  } else
    	   return true;
    	 }
}
